﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.Com.UI.Interface;
using ScintillaNET;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill
{
    public partial class BillReflectJPluginPanel : XtraUserControl, ProjectUnitBillInterface
    {
        private ProjectUnitBillInterface parentPage = null;
        private ProjectRootPanel RootPanel = null;
        private bool save = true;

        private BillExpandJavaCodeItem java = null;

        public BillReflectJPluginPanel(ProjectRootPanel Root, ProjectUnitBillInterface pRoot, BillExpandJavaCodeItem code, bool Save)
        {
            InitializeComponent();
            RootPanel = Root;
            parentPage = pRoot;
            save = Save;
            java = code;
            InitForm();
        }

        public string BillKey
        {
            get
            {
                return java.Key;
            }
        }

        public bool isSubUnitBill
        {
            get
            {
                return true;
            }
        }


        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="clazz">复制后的clazz</param>
        /// <returns></returns>
        public bool Copy(out object clazz)
        {
            bool val = false;
            clazz = null;

            return val;
        }

        public ProjectUnitBillInterface ParentPage
        {
            get
            {
                return parentPage;
            }

            set
            {
                parentPage = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return parentPage.ReadOnly;
            }

            set
            {
                
            }
        }

        public bool SaveStatus
        {
            get
            {
                return save;
            }

            set
            {
                save = value;
                RootPanel.SaveStatus = value;
            }
        }

        public List<ProjectUnitBillInterface> SubjectPage
        {
            get
            {
                return null;
            }
        }

        public void AddSubjectBill(ProjectUnitBillInterface bill)
        {
            
        }

        public bool close()
        {
            bool result = true;
            if (!save)
            {
                DialogResult DR = XtraMessageBox.Show("当前数据已更改是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (DR == DialogResult.OK)
                {
                    this.saveData(true);
                }
                else if (DR == DialogResult.Cancel)
                {
                    result = false;
                }
            }
            if (result && parentPage != null)
            {
                parentPage.RemoveSubjectBill(this);
                parentPage = null;
            }
            return result;
        }

        public bool ContainSubject(ProjectUnitBillInterface bill)
        {
            return false;
        }

        public void RemoveSubjectBill(ProjectUnitBillInterface bill)
        {
            
        }

        public void saveData(bool updateParent)
        {
            if (parentPage.ReadOnly) return;
            java.Content = JavaMemo.Text;
            parentPage.SaveStatus = false;
            if (updateParent)
            {
                SaveStatus = true;
            }
            else
            {
                save = true;
            }
        }

        private void InitForm()
        {
            #region 初始编辑器
            JavaMemo.Margins[0].Width = 20;
            JavaMemo.Margins[2].Type = MarginType.Symbol;
            JavaMemo.Margins[2].Mask = Marker.MaskFolders;
            JavaMemo.Margins[2].Sensitive = true;
            JavaMemo.Margins[2].Width = 20;

            JavaMemo.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
            JavaMemo.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
            JavaMemo.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
            JavaMemo.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
            JavaMemo.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
            JavaMemo.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
            JavaMemo.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

            JavaMemo.StyleResetDefault();
            JavaMemo.Styles[Style.Default].Font = "Consolas";
            JavaMemo.Styles[Style.Default].Size = 10;
            JavaMemo.StyleClearAll();


            // Configure the CPP (C#) lexer styles
            JavaMemo.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
            JavaMemo.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JavaMemo.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JavaMemo.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
            JavaMemo.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
            JavaMemo.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
            JavaMemo.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
            JavaMemo.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JavaMemo.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JavaMemo.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JavaMemo.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
            JavaMemo.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
            JavaMemo.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;
            JavaMemo.Lexer = Lexer.Cpp;

            // Set the keywords
            JavaMemo.SetKeywords(0, "function abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe import virtual while");
            JavaMemo.SetKeywords(1, "boolean var byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

            //JavaMemo.SetKeywords(0, common.unit.Label.SQLKeys);
            #endregion

            JavaMemo.Text = java.Content;
        }

        private void JavaMemo_CharAdded(object sender, CharAddedEventArgs e)
        {
            SaveStatus = false;
        }

        private void JavaMemo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
            {
                SaveStatus = false;
            }
            else if (e.KeyCode == Keys.V && e.Control && Clipboard.ContainsText())
            {
                SaveStatus = false;
            }
        }

        private void JavaMemo_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
